I have defined a route action with some business logic, inside an internally developed package. Depending on the result in this action, the app want to redirect the user to some dynamic route (Redirect::route('admin.index', [$app->id]) e.g).
How would I do this?
Any solution I come up with doesn't work because of the way Laravel handles routes.
Right now I have copied the route to the app routes.php, and extracted the business logic to a method inside the package. But this is not optimal, as I'd like to also keep the route inside the package.
Laravel has some documentation on Package Configuration that should work for you.
In your package's src/config/config.php:
<?php
return array(
'route_admin_index' => 'admin.index',
);
Change your package's code to:
Redirect::route(Config::get('your_package_name::route_admin_index'), [$app->id]);
Now when installed on different environments, you can do:
php artisan config:publish your_vendor_name/your_package_name
Which will publish your package's configuration file to:
app/config/packages/your_vendor_name/your_package_name
Where then you can change the route_admin_index at will.
If php artisan config:publish was not called. Your route will default back to what you have in your package's config file.
Related
I wanna change default Jetstream components after publishing views (php artisan vendor:publish --tag=jetstream-views) from resources/views/vendor/jetstream/components for example to resources/views/path/to/jet/comps
You can do this by copying the components from laravel/jetstream and putting them wherever you like within your app.
Then you need to alias the jetstream class with your own version.
What I did was copy all components from
/vendor/laravel/jetstream/src/Http/livewire/
Copied them to
/app/Http/Livewire/
Then open your app/Providers/JetstreamSrviceProvider.php or if you would prefer you can create your own service provider or use AppServiceProvider.php.
Add the following before the class definition but after the namespace definition:
use Illuminate\Foundation\AliasLoader;
Then in the register method add this:
$loader = AliasLoader::getInstance();
$loader->alias('Laravel\Jetstream\Http\Livewire\ApiTokenManager', 'App\Http\Livewire\ApiTokenManager');
$loader->alias('Laravel\Jetstream\Http\Livewire\CreateTeamForm', 'App\Http\Livewire\CreateTeamForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\DeleteTeamForm', 'App\Http\Livewire\DeleteTeamForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\DeleteUserForm', 'App\Http\Livewire\DeleteUserForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\LogoutOtherBrowserSessionsForm', 'App\Http\Livewire\LogoutOtherBrowserSessionsForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\NavigationDropdown', 'App\Http\Livewire\NavigationDropdown');
$loader->alias('Laravel\Jetstream\Http\Livewire\TeamMemberManager', 'App\Http\Livewire\TeamMemberManager');
$loader->alias('Laravel\Jetstream\Http\Livewire\TwoFactorAuthenticationForm', 'App\Http\Livewire\TwoFactorAuthenticationForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\UpdatePasswordForm', 'App\Http\Livewire\UpdatePasswordForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\UpdateProfileInformationForm', 'App\Http\Livewire\UpdateProfileInformationForm');
$loader->alias('Laravel\Jetstream\Http\Livewire\UpdateTeamNameForm', 'App\Http\Livewire\UpdateTeamNameForm');
Then run
composer dump-autoload
You can then open your version of the components and modify the view paths if you prefer or whatever else it is you were looking to do.
I have built a CMS as a package and done exactly this from a package which works well and means we can ship the package with our own views and updated components.
i wrote url like that
use App\Http\Controllers\DashController;
Route::get('/admin/special', [DashController::class, 'addSpecializations'])->name('add.sp');
this is controller
public function addSpecializations()
{
return view('dashboard.add-specializations');
}
when i tried to open it i can't even though all route work
after that i wrote this code in view's file
<a href="{{route('add.sp')}}">
so i faced this issue
Route [add.sp] not defined.
In case your routes are cached, run php artisan route:clear. In development, don't cache anything, including views and config.
Here is the problem.
I started a dingo project and am using laravel-modules in it. Every module has its own routing files. Using the project in development environment, everything works fine.
But when I run php artisan config:cache, when a request comes to laravel, it return the response The version given was unknown or has no registered routes. As I see, the problem is dingo just check the default api.php and web.php files to find the route. But module routes are not stored in that files. I store them in Modules/module_name/route/api.php file (as laravel-modules suggested).
Any suggestion would be welcome.
change api file of module with version param in group session like this:
$api = app('Dingo\Api\Routing\Router');
$api->group(['version' => 'v1'], function ($api) {
...
});
Is there any possibility to turn on and turn off Laravel 5 maintenance without php artisan up and down commands when my website is being hosted ?
What I've done:
Route::get('site/shutdown', function(){
return Artisan::call('down');
});
Route::get('site/live', function(){
return Artisan::call('up');
});
The first route is working fine. But when I call site/live the site still is shuted down. What can cause this problem ?
If your project is already down, you cannot call another function.
What happens after you run php artisan down is that it creates a file named down inside storage/framework. After running php artisan up the file is removed.
You can create the file manually inside storage/framework. It will down your project. When you want to take your project live again, just remove the file.
I think the right answer is missing here..
You could add your route to app/http/middleware/CheckForMaintenanceMode.php
protected $except = [
//here
];
So It never would be off.
when you run artisan down. site is not available so when try to call up, your IP can't access site.
you must call down with your IP exception.
php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16
or add ::1 to local.
to make that in route without command
try to save this command in specific one and call it.
Laravel 8 introduced secret in maintenance mode, in which you can bypass the maintenance mode by providing a secret, then your Artisan::call would work.
You could add your routes to the $except var in CheckForMaintenanceMode middleware to bypass the check. Then your site/live route would work just fine.
In order to make your site live again using an url, you can create a live.php file which you put in laravel's public folder and then visit http://your.domain/live.php .
In the live.php file you need something like this: (check your projects directory structure if you don't use the default public folder!)
<?php
unlink(dirname(__FILE__) . "/../storage/framework/down");
header("Location: your.domain");
die;
just put
Artisan::call('up');
without route function.
In a Laravel 4 installation, Using Jeffrey Way's Laravel 4 Generators, I set up a 'tweet' resource, using the scaffolding command from his example:
php artisan generate:scaffold tweet --fields="author:string, body:text"
This generated the model, view, controller, migration and routing information for the tweet type. After migrating the database, visiting http://localhost:8000/tweets works fine, and shows the expected content.
The contents of the routes.php file at this point is:
Route::resource('tweets', 'TweetsController');
Now I would like to move the url for tweets up one level into admin/tweets, so the above url should become: http://localhost:8000/admin/tweets. Please note that I am not treating 'Admin' as a resource, but instead just want to add it for hypothetical organizational purposes.
Changing the routes.php file to:
Route::resource('admin/tweets', 'TweetsController');
Does not work, and displays the following error:
Unable to generate a URL for the named route "tweets.create" as such route does not exist.
Similarly when using the following:
Route::group(array('prefix' => 'admin'), function() {
Route::resource('tweets', 'TweetsController');
});
As was suggested in this stackoverflow question.
Using php artisan routes reveals that the named routes also now have admin prefixed to them, turning tweets.create into admin.tweets.create.
Why is the error saying that it cannot find tweets.create? shouldn't that automatically be resolved (judging by the routes table), to use admin.tweets.create?
How can I change my routing so that this error no longer occurs?
I just tested with new resource controller and it works fine for me.
The problem is not with the Route, its with the named routes used in your application.
check your view files there are link to route like link_to_route('tweets.create', 'Add new tweet'), this is creating the error because when you add admin as prefix tweets.create doesn't exists so change it to admin.tweets.create every where, in your controller also where ever named route is used.